package com.alasbusy.alasbusyapp.activity.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.FloatingActionButton;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.alasbusy.alasbusyapp.R;
import com.alasbusy.alasbusyapp.activity.QrCodeActivity;
import com.alasbusy.alasbusyapp.base.BaseFragment;
import com.alasbusy.alasbusyapp.utils.CircleImageView;
import com.alasbusy.alasbusyapp.utils.ScreenSizeUtils;
import com.zhy.android.percent.support.PercentLinearLayout;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/14.
 */

public class RewardFragment extends BaseFragment implements View.OnClickListener {

    private PercentRelativeLayout zhichi_shu_ye;
    private TextView guozi_center;
    private TextView guizi_right_01;
    private TextView guizi_right_02;
    private TextView guizi_right_03;
    private TextView guizi_right_04;
    private TextView guizi_right_05;
    private TextView guizi_right_06;
    private TextView guizi_right_07;
    //private TextView guizi_right_08;
    private TextView guizi_right_09;
    private TextView guizi_right_10;
    private TextView guizi_right_11;
    private TextView guizi_right_12;
    private TextView guizi_right_13;
    //private TextView guizi_right_14;
    private TextView guizi_right_15;
    private CircleImageView popwind_img;

    private List<TextView> mlist = new ArrayList<>();
    private List<String> mData = new ArrayList<>();
    private List<String> mContent = new ArrayList<>();
    private PopupWindow window;
    private FloatingActionButton reward_action_btn;

    @Override
    public int getLayout() {
        return R.layout.reward_fragment;
    }

    @Override
    protected void initView(View view) {
        PercentLinearLayout reward_fragment_title = view.findViewById(R.id.reward_fragment_title);
        setTop(reward_fragment_title);
        getVisibility(false, getActivity());
        getTitle("支持");

        zhichi_shu_ye = view.findViewById(R.id.zhichi_shu_ye);

        guozi_center = view.findViewById(R.id.guozi_center);
        guizi_right_01 = view.findViewById(R.id.guizi_right_01);
        guizi_right_02 = view.findViewById(R.id.guizi_right_02);
        guizi_right_03 = view.findViewById(R.id.guizi_right_03);
        guizi_right_04 = view.findViewById(R.id.guizi_right_04);
        guizi_right_05 = view.findViewById(R.id.guizi_right_05);
        guizi_right_06 = view.findViewById(R.id.guizi_right_06);
        guizi_right_07 = view.findViewById(R.id.guizi_right_07);
        //guizi_right_08 = view.findViewById(R.id.guizi_right_08);
        guizi_right_09 = view.findViewById(R.id.guizi_right_09);
        guizi_right_10 = view.findViewById(R.id.guizi_right_10);
        guizi_right_11 = view.findViewById(R.id.guizi_right_11);
        guizi_right_12 = view.findViewById(R.id.guizi_right_12);
        guizi_right_13 = view.findViewById(R.id.guizi_right_13);
        //guizi_right_14 = view.findViewById(R.id.guizi_right_14);
        guizi_right_15 = view.findViewById(R.id.guizi_right_15);

        reward_action_btn = view.findViewById(R.id.reward_action_btn);

        mlist.add(guozi_center);
        mlist.add(guizi_right_01);
        mlist.add(guizi_right_02);
        mlist.add(guizi_right_03);
        mlist.add(guizi_right_04);
        mlist.add(guizi_right_05);
        mlist.add(guizi_right_06);
        mlist.add(guizi_right_07);
        //mlist.add(guizi_right_08);
        mlist.add(guizi_right_09);
        mlist.add(guizi_right_10);
        mlist.add(guizi_right_11);
        mlist.add(guizi_right_12);
        mlist.add(guizi_right_13);
        //mlist.add(guizi_right_14);
        mlist.add(guizi_right_15);
    }

    @Override
    protected void initData() {
        for (int i = 0; i < 14; i++) {
            mData.add(i+1+"."+i);
            mContent.add("罗伊伞可"+i);
        }
    }

    @Override
    protected void initListener() {
        /*guozi_center.setOnClickListener(this);
        guizi_right_01.setOnClickListener(this);
        guizi_right_02.setOnClickListener(this);
        guizi_right_03.setOnClickListener(this);
        guizi_right_04.setOnClickListener(this);
        guizi_right_05.setOnClickListener(this);
        guizi_right_06.setOnClickListener(this);
        guizi_right_07.setOnClickListener(this);
        guizi_right_08.setOnClickListener(this);
        guizi_right_09.setOnClickListener(this);
        guizi_right_10.setOnClickListener(this);
        guizi_right_11.setOnClickListener(this);
        guizi_right_12.setOnClickListener(this);
        guizi_right_13.setOnClickListener(this);
        guizi_right_14.setOnClickListener(this);
        guizi_right_15.setOnClickListener(this);*/

        reward_action_btn.setOnClickListener(this);

        for (int i = 0; i < mlist.size(); i++) {
            final TextView tv = mlist.get(i);
            final String s = mData.get(i);
            final String content = mContent.get(i);
            tv.setText(s);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ShowPopWindow(tv,"",content + "支持¥" + s);
                }
            });
        }


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.reward_action_btn:
                Dialog dialog = new Dialog(getContext(), R.style.NormalDialogStyle);
                View dialogView = View.inflate(getContext(), R.layout.dialog_bottom_item, null);
                dialog.setContentView(dialogView);
                dialog.setCanceledOnTouchOutside(true);
                dialogView.setMinimumHeight((int) (ScreenSizeUtils.getInstance(getContext()).getScreenHeight() * 0.23f));
                Window dialogWindow = dialog.getWindow();
                WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                lp.width = (int) (ScreenSizeUtils.getInstance(getContext()).getScreenWidth() * 0.9f);
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                lp.gravity = Gravity.BOTTOM;
                dialogWindow.setAttributes(lp);
                dialog.show();
                setDialogClick(dialog,dialogView);
                break;

        }
    }

    private void setDialogClick(final Dialog dialog, View dialogView) {
        Button alipay = dialogView.findViewById(R.id.alipay);
        Button weoxin = dialogView.findViewById(R.id.weoxin);
        Button close = dialogView.findViewById(R.id.close);
        alipay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Intent intent = new Intent(getContext(), QrCodeActivity.class);
                intent.putExtra("type","alipay");
                getActivity().startActivity(intent);
            }
        });
        weoxin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                Intent intent = new Intent(getContext(), QrCodeActivity.class);
                intent.putExtra("type","weoxin");
                getActivity().startActivity(intent);
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
    }

    public void ShowPopWindow(View view, String img, String content) {
        View popupView = getActivity().getLayoutInflater().inflate(R.layout.popupwindow, null);

        CircleImageView popwind_img = popupView.findViewById(R.id.popwind_img);
        TextView popwind_content = popupView.findViewById(R.id.popwind_content);
        popwind_content.setText(content);
        // 创建PopupWindow对象，指定宽度和高度
        window = new PopupWindow(popupView, 920, 800);
        // 设置动画
        //window.setAnimationStyle(R.style.popup_window_anim);
        // 设置背景颜色
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置可以获取焦点
        window.setFocusable(true);
        // 设置可以触摸弹出框以外的区域
        window.setOutsideTouchable(true);
        // 更新popupwindow的状态
        window.update();
        //以下拉的方式显示，并且可以设置显示的位置
        window.showAsDropDown(view, 0, 0);
    }

}
